# -*- coding: utf-8 -*-
"""
Created on Thu May  9 14:38:20 2019

@author: yaoyu
"""

import pandas as pd
import numpy as np

# 从二维数组
d = pd.DataFrame(np.arange(10).reshape((2,5)))
print(d)

# 从一维ndarray对象字典创建
dt = {'one':pd.Series([1,2,3], index=['a','b','c']),
      'two':pd.Series([9,8,7,6], index=['a','b','c', 'd'])}
d = pd.DataFrame(dt)
print(d)

d = pd.DataFrame(dt, index=['b','c', 'd'], columns=['two', 'three'])
print(d)

# 列表类型的字典
dl = {'one': [1,2,3,4], 'two':[9,8,7,6]}
d = pd.DataFrame(dl, index=['a','b','c','d'])
print(d)

dl = {'城市':['北京','上海','广州','深圳','沈阳'],
      '环比':[101.5, 101.2, 101.3, 102.0, 100.1],
      '同比':[120.7, 127.3, 119.4, 140.9, 101.4],
      '定基':[121.4, 127.8, 120.0, 145.5, 101.6]
      }
d = pd.DataFrame(dl, index=['c1','c2','c3','c4','c5'])
print(d)
print(d.index)
print(d.columns)
print(d.values)
print(d['同比'])
print(d['同比']['c2'])
# print(d.ix['c2'])

d = d.reindex(index=list(reversed(['c1','c2','c3','c4','c5'])))
print(d)

d = d.reindex(columns = ['城市', '同比', '环比', '定基'])
print(d)

newc = d.columns.insert(4, '新增')
newd = d.reindex(columns=newc, fill_value=200)
print(newd)

nc = d.columns.delete(2)
ni = d.index.insert(5, 'c0')
nd = d.reindex(index=ni, columns=nc, method='ffill')
print(nd)